import { Component, OnInit, Renderer2 } from '@angular/core';
import { Store } from '@ngrx/store';
import { UserLoginSuccess, State } from './store';
import { UserToken } from './shared/modes';
import { LocalstoreService } from './shared/services';

@Component({
  selector: 'ege-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  
  constructor(
    private store: Store<State>,
    private localstorage: LocalstoreService,
    private renderer: Renderer2
  ) {
  }

  ngOnInit(): void {
    this.removeLoadingSpinner();
    this.loadUserToken();
  }

  removeLoadingSpinner(): void {
    const spinner: HTMLElement = document.getElementById('global-spinner');
    this.renderer.setStyle(spinner, 'display', 'none');
  }

  loadUserToken(): void {
    const userToken: UserToken = this.localstorage.getItem('USER_TOKEN');
    userToken && this.store.dispatch(new UserLoginSuccess(userToken));
  }

}
